package com.weseal.dry.config;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import common.support.CustomRepositoryFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.WriteResultChecking;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.Collections;


/**
 * @title
 * @description
 * @usage
 * @copyright Copyright 2014  rrmj Corporation. All rights reserved.
 * @company 上海众多美网络科技有限公司
 * @author weseal
 * @create 2018/3/27
 */
@Configuration
@EnableMongoRepositories(value = "com.weseal.dry.repository",
		repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class)

public class DataConfig extends AbstractMongoConfiguration {


	@Override
	public MongoClient mongoClient() {
		  return new MongoClient(Collections.singletonList(new ServerAddress("111.231.145.36", 27017)),
				Collections.singletonList(MongoCredential.createCredential("test", "test", "test".toCharArray())));
	}

	@Override
	protected String getDatabaseName() {
		return "test";
	}

	@Override
	@Bean
	public MongoTemplate mongoTemplate() throws Exception {
		MongoTemplate mongoTemplate = super.mongoTemplate();
		mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
		return mongoTemplate;
	}



//	@Override
//	public MappingMongoConverter mappingMongoConverter() throws Exception {
//		MappingMongoConverter mappingMongoConverter = super.mappingMongoConverter();
//		MongoCustomConversions customConversions =  new MongoCustomConversions();
//		mappingMongoConverter.setCustomConversions();
//	}
}
